﻿using Autofac.Core;
using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using DongFangGuoKai.WMSPlatform.Service.OutboundOrderDetail;
using DongFangGuoKai.WMSPlatform.Service.OutboundOrderDetail.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Security.Claims;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers.OrderOutbound
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TShippingOrderDetailController : BaseController
    {
        private readonly ITOutboundOrderDetailService tOutboundOrderDetailService;
        private readonly ILogger<TShippingOrderDetailController> shippingorderlogger;

        public TShippingOrderDetailController(ITOutboundOrderDetailService tOutboundOrderDetailService,
            ILogger<TShippingOrderDetailController> shippingorderlogger
            )
        {
            this.tOutboundOrderDetailService = tOutboundOrderDetailService;
            this.shippingorderlogger = shippingorderlogger;
        }
        #region PC

        /// <summary>
        /// 添加出库表 多个物料版本
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTShippingOrderDetailPlus(TShippingOrderDetailPlusDTO model)
        {
            var create = tOutboundOrderDetailService.AddTShippingOrderDetailPlus(model, out var msg);
            if (create)
            {
                //string uname = HttpContext.User.FindFirstValue("UserName");
                //string runame = HttpContext.User.FindFirstValue("RealName");
                //int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                //string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                shippingorderlogger.LogInformation("添加出库单成功" + msg + "入参，" + model.ToJson());
                return Success("添加成功");
            }
            shippingorderlogger.LogError("添加出库单失败" + msg + "入参，" + model.ToJson());
            return Failed(msg);
        }


        /// <summary>
        /// 显示出库表
        /// </summary>
        /// <param name="billType"></param>
        /// <param name="houseid"></param>
        /// <param name="name"></param>
        /// <param name="statusNum"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllTShippingOrderDetailPlus(int? billType, int? houseid, string? name, int? statusNum, int pageIndex = 1, int pageSize = 8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Failed( "分页条件有误！");
            }
            int totalCount;

            var list = tOutboundOrderDetailService.GetAllTShippingOrderDetailPlus(billType, houseid, name, pageIndex, pageSize, out totalCount, statusNum);
            if (list.Count >= 0)
            {
                return Success(list, totalCount,"查询成功");
            }
            return Failed("查询失败！");
        }

        /// <summary>
        /// 显示单个出库表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOneTShippingOrderDetailPlus()
        {
            var resObje = tOutboundOrderDetailService.GetOneTShippingOrderDetailPlus(out bool res);
            if (res && resObje != null)
            {
                return Success(resObje, "查询成功！");
            }
            return Failed( "查询失败！");
        }

        /// <summary>
        /// 修改出库单状态为已回传
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdoutShippingStatus(string orderSn)
        {
            var res = tOutboundOrderDetailService.UpdOutShippingStatus(orderSn, out var errorMsg);
            if (res)
            {
                shippingorderlogger.LogInformation("修改出库单状态成功" + errorMsg + "入参，" + orderSn);
                return Success("修改成功");
            } 
            shippingorderlogger.LogError("修改出库单状态失败" + errorMsg + "入参，" + orderSn);
            return Failed("修改失败：" + errorMsg);
        }
        #endregion

        #region PDA
        /// <summary>
        /// 获取出库单明细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTShippingOrderDetailPlus(int pageIndex, int pageSize, int isNetWork)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Failed( "分页条件有误！");
            }
            var list = tOutboundOrderDetailService.GetTShippingOrderDetailPlus(pageIndex, pageSize, isNetWork);
            if (list != null)
            {
                return Success(list, "查询成功！");
            }
            return Failed( "未查询到出库信息！",200);
        }

        /// <summary>
        /// 出库复核 最终确定按钮
        /// </summary>
        /// <param name="OrderDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitOutboudOrder(SubmitOutListDTONoNet OrderDTO)
        {
            string message;
            try
            {
                var resOut = tOutboundOrderDetailService.ChukuMsg(OrderDTO, out message);
                if (resOut)
                {
                    shippingorderlogger.LogInformation("出库成功" + message + "入参，" + OrderDTO.ToJson());
                    return Success( "出库解绑成功");
                }
                else
                {
                    shippingorderlogger.LogError("出库失败" + message + "入参，" + OrderDTO.ToJson());
                    return Failed( message);
                }
            }
            catch (Exception ex)
            {
                return Failed("出库失败，" + ex.Message);
            }

        }
        #endregion
    }
}
